package site.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.n3r.eql.Eql;
import org.n3r.eql.EqlPage;
import org.springframework.stereotype.Service;

import site.util.Collections;

@Service
public class WorksService {
    public Map queryWorksList(EqlPage eqlPage) {
        Map resMap = new HashMap();
        List<Map> worksList = new Eql().id("queryWorksList").params(new HashMap()).returnType(HashMap.class)
                .limit(eqlPage).execute();
        //        List<Map> worksList = new Eql().select("queryWorksList").params(inMap).returnType(HashMap.class).execute();
        //        Integer totalNum = new Eql().selectFirst("queryTotalNum").returnType(Integer.class).limit(1).execute();
        Map pageInfo = Collections.asMap("totalPages", eqlPage.getTotalPages(), "totalSize", eqlPage.getTotalRows(),
                "currentPage", eqlPage.getCurrentPage());
        resMap.put("worksList", worksList);
        resMap.put("pageInfo", pageInfo);
        return resMap;
    }

    public Map queryWorksInfoList(String worksType) {
        Map resMap = new HashMap();
        List<Map> worksInfoList = new Eql().select("queryWorksInfoList").params(worksType).returnType(HashMap.class)
                .execute();
        List<Map> worksNumList = new Eql().select("queryWorksNum").returnType(HashMap.class).execute();
        resMap.put("worksType", worksType);
        resMap.put("worksNumList", worksNumList);
        resMap.put("worksInfoList", worksInfoList);
        return resMap;
    }

    public Map queryWorksInfo(Map inMap) {
        Map resMap = new HashMap();
        Map worksInfoMap = new Eql().selectFirst("queryWorksList").params(inMap).returnType(HashMap.class).execute();
        List<Map> worksTypeList = new Eql().select("queryWorksPara").params("1000").returnType(HashMap.class).execute();
        List<Map> worksYearsList = new Eql().select("queryWorksPara").params("1001").returnType(HashMap.class)
                .execute();
        List<Map> worksFormList = new Eql().select("queryWorksPara").params("1002").returnType(HashMap.class).execute();
        List<Map> worksMaterialList = new Eql().select("queryWorksPara").params("1003").returnType(HashMap.class)
                .execute();
        resMap.put("worksTypeList", worksTypeList);
        resMap.put("worksYearsList", worksYearsList);
        resMap.put("worksFormList", worksFormList);
        resMap.put("worksMaterialList", worksMaterialList);
        resMap.put("worksInfoMap", worksInfoMap);
        return resMap;
    }

    public Map queryWorksPara() {
        Map resMap = new HashMap();
        List<Map> worksTypeList = new Eql().select("queryWorksPara").params("1000").returnType(HashMap.class).execute();
        List<Map> worksYearsList = new Eql().select("queryWorksPara").params("1001").returnType(HashMap.class)
                .execute();
        List<Map> worksFormList = new Eql().select("queryWorksPara").params("1002").returnType(HashMap.class).execute();
        List<Map> worksMaterialList = new Eql().select("queryWorksPara").params("1003").returnType(HashMap.class)
                .execute();
        resMap.put("worksTypeList", worksTypeList);
        resMap.put("worksYearsList", worksYearsList);
        resMap.put("worksFormList", worksFormList);
        resMap.put("worksMaterialList", worksMaterialList);
        return resMap;
    }

    public int setWorksBanner(String worksId) {
        Integer i = new Eql().update("setWorksBanner").params(worksId).execute();
        return i;
    }

    public int delWorksBanner(String worksId) {
        Integer i = new Eql().update("delWorksBanner").params(worksId).execute();
        return i;
    }

    public int delWorks(String worksId) {
        Integer i = new Eql().update("delWorks").params(worksId).execute();
        return i;
    }

    public int addWorks(Map inMap) {
        Integer i = new Eql().insert("addWorks").params(inMap).execute();
        return i;
    }

    public int editWorksSubmit(Map inMap) {
        Integer i = 0;
        if ("1".equals(MapUtils.getString(inMap, "uploadFlag"))) {
            i = new Eql().update("editWorksWithPicSubmit").params(inMap).execute();
        } else {
            i = new Eql().update("editWorksNoPicSubmit").params(inMap).execute();
        }
        return i;
    }
}
